package o2o.area;

import o2o.cache.area.CityMemory;
import o2o.cache.area.CountyMemory;
import o2o.cache.area.ProvinceMemory;
import o2o.config.dao.DataDaoSql;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * user: Jahnke [1029777564@qq.com]
 * Date: 2016-11-25 20:48
 * 中国省市区县缓存数据
 */
public class ChnAreaMemory {
    private ChnAreaMemory() {
    }

    private static ChnAreaMemory single = new ChnAreaMemory();

    public static ChnAreaMemory getInstance() {
        if (single == null) {
            single = new ChnAreaMemory();
        }
        return single;
    }

    // private static String chn_province = "chn_province";
    // private static String chn_city = "chn_city";
    // private static String chn_county = "chn_county";

    private HashMap<String, ProvinceInfo> dataMap = new HashMap<String, ProvinceInfo>();        //省份存储

    //启动加载缓存数据
    public void run() {
        dataMap.clear();


        // String provSql = "select * from " + chn_province;
        // String citySql = "select * from " + chn_city;
        // String countySql = "select * from " + chn_county;

        try {
            // List<ProvinceInfo> provs = (List<ProvinceInfo>) DataDaoSql.getInstance().list(ProvinceInfo.class, provSql);
            // List<CityInfo> cities = (List<CityInfo>) DataDaoSql.getInstance().list(CityInfo.class, citySql);
            // List<CountyInfo> counties = (List<CountyInfo>) DataDaoSql.getInstance().list(CountyInfo.class, countySql);
            List<ProvinceInfo> provs = ProvinceMemory.getInstance().list(null);
            List<CityInfo> cities = CityMemory.getInstance().list(null, null);
            List<CountyInfo> counties = CountyMemory.getInstance().list(null, null);

            for (int i = 0; i < provs.size(); i++) {
                dataMap.put(provs.get(i).getProvCode(), provs.get(i));
            }

            HashMap<String, CityInfo> cityMap = new HashMap<String, CityInfo>();
            for (int i = 0; i < cities.size(); i++) {
                cityMap.put(cities.get(i).getCityCode(), cities.get(i));
            }

            HashMap<String, List<CountyInfo>> countyMap = new HashMap<String, List<CountyInfo>>();
            for (int i = 0; i < counties.size(); i++) {
                List<CountyInfo> clis;
                if (countyMap.containsKey(counties.get(i).getCityCode())) {
                    clis = countyMap.get(counties.get(i).getCityCode());
                } else {
                    clis = new ArrayList<CountyInfo>();
                }
                clis.add(counties.get(i));
                countyMap.put(counties.get(i).getCityCode(), clis);
            }

            for (String cityCode : cityMap.keySet()) {
                if (countyMap.containsKey(cityCode)) {
                    cityMap.get(cityCode).setCounties(countyMap.get(cityCode));
                }
            }

            for (String cityCode : cityMap.keySet()) {
                if (dataMap.containsKey(cityMap.get(cityCode).getProvCode())) {
                    List<CityInfo> lis = dataMap.get(cityMap.get(cityCode).getProvCode()).getCities();
                    if (lis == null) {
                        lis = new ArrayList<CityInfo>();
                    }
                    lis.add(cityMap.get(cityCode));
                    dataMap.get(cityMap.get(cityCode).getProvCode()).setCities(lis);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取所有地区
    public List<ProvinceInfo> getAll() {
        List<ProvinceInfo> list = new ArrayList<ProvinceInfo>();
        for (String key : dataMap.keySet()) {
            list.add(dataMap.get(key));
        }

        return list;
    }

    //获取省份的地区
    public ProvinceInfo get(String province) {
        for (String key : dataMap.keySet()) {
            if (dataMap.get(key).getProvince().indexOf(province) >= 0 || province.indexOf(dataMap.get(key).getProvince()) >= 0) {
                return dataMap.get(key);
            }
        }
        return null;
    }

}
